package top.codedance.iotp.tools.serverTool.ui;

import top.codedance.iotp.common.util.HexCodec;
import top.codedance.iotp.tools.terminal.ui.ShellFrame;

import javax.swing.*;
import javax.xml.bind.DatatypeConverter;
import java.awt.*;
import java.net.URL;

/**
 *
 * @author xuedianfu
 */
public class Hex2AsciiFrame extends javax.swing.JDialog {
    private MainFrame mf;
    private ShellFrame sf;
    private HexCodec hc;

    /**
     * Creates new form Hex2AsciiFrame
     */
    public Hex2AsciiFrame(MainFrame mf) {
        super(mf, false);
        this.mf = mf;
        init();
    }

    public Hex2AsciiFrame(ShellFrame sf) {
        super(sf, false);
        this.sf = sf;
        init();
    }

    private void init(){
        initComponents();
        URL url = Thread.currentThread().getContextClassLoader().getResource("logo.gif");
        ImageIcon icon = new ImageIcon(url);
        setIconImage(icon.getImage());
        setMinimumSize(new Dimension(1062, 643));
        setLocationRelativeTo(this.mf);
        setTitle("代码飞舞采控系统-Hex-ASCII互转");
        hc = new HexCodec();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jta_ascii = new javax.swing.JTextArea();
        jb_ascii2hex = new javax.swing.JButton();
        jb_hex2ascii = new javax.swing.JButton();
        jScrollPane2 = new javax.swing.JScrollPane();
        jta_hex = new javax.swing.JTextArea();
        jb_cls = new javax.swing.JButton();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jb_stringToHex = new javax.swing.JButton();
        jb_hexToString = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jta_ascii.setColumns(20);
        jta_ascii.setRows(5);
        jScrollPane1.setViewportView(jta_ascii);

        jb_ascii2hex.setText("ASCII转HEX");
        jb_ascii2hex.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jb_ascii2hexActionPerformed(evt);
            }
        });

        jb_hex2ascii.setText("HEX转AXCII");
        jb_hex2ascii.setToolTipText("");
        jb_hex2ascii.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jb_hex2asciiActionPerformed(evt);
            }
        });

        jta_hex.setColumns(20);
        jta_hex.setRows(5);
        jScrollPane2.setViewportView(jta_hex);

        jb_cls.setText("清空内容");
        jb_cls.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jb_clsActionPerformed(evt);
            }
        });

        jLabel1.setText("ASCii | String：");

        jLabel2.setText("HEX：");

        jb_stringToHex.setText("字符串转十六进制字符串");
        jb_stringToHex.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jb_stringToHexActionPerformed(evt);
            }
        });

        jb_hexToString.setText("十六进制字符串还原字符串");
        jb_hexToString.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jb_hexToStringActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jScrollPane1)
                    .addComponent(jScrollPane2)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel1)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel2)
                                .addGap(18, 18, 18)
                                .addComponent(jb_ascii2hex)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jb_hex2ascii)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jb_cls)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jb_stringToHex)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jb_hexToString)))
                        .addGap(0, 82, Short.MAX_VALUE)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 203, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jb_ascii2hex)
                    .addComponent(jb_hex2ascii)
                    .addComponent(jb_cls)
                    .addComponent(jLabel2)
                    .addComponent(jb_stringToHex)
                    .addComponent(jb_hexToString))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane2, javax.swing.GroupLayout.DEFAULT_SIZE, 249, Short.MAX_VALUE)
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jb_ascii2hexActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jb_ascii2hexActionPerformed
        this.jta_hex.setText(hc.toHex(this.jta_ascii.getText().trim().getBytes()));
    }//GEN-LAST:event_jb_ascii2hexActionPerformed

    private void jb_hex2asciiActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jb_hex2asciiActionPerformed
        this.jta_ascii.setText(new String(hc.toBytes(this.jta_hex.getText().trim())));
    }//GEN-LAST:event_jb_hex2asciiActionPerformed

    private void jb_clsActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jb_clsActionPerformed
        this.jta_hex.setText("");
        this.jta_ascii.setText("");
    }//GEN-LAST:event_jb_clsActionPerformed

    private void jb_stringToHexActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jb_stringToHexActionPerformed
        this.jta_hex.setText(DatatypeConverter.printHexBinary(this.jta_ascii.getText().trim().getBytes()));
    }//GEN-LAST:event_jb_stringToHexActionPerformed

    private void jb_hexToStringActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jb_hexToStringActionPerformed
        this.jta_ascii.setText(new String(DatatypeConverter.parseHexBinary(this.jta_hex.getText().trim())));
    }//GEN-LAST:event_jb_hexToStringActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JButton jb_ascii2hex;
    private javax.swing.JButton jb_cls;
    private javax.swing.JButton jb_hex2ascii;
    private javax.swing.JButton jb_hexToString;
    private javax.swing.JButton jb_stringToHex;
    private javax.swing.JTextArea jta_ascii;
    private javax.swing.JTextArea jta_hex;
    // End of variables declaration//GEN-END:variables
}
